#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/1/3 19:27
# @Author : HuangBenHao
import requests
from flask import Flask, request, jsonify
from flask_cors import CORS
from py2neo import Graph

from search_entity import search_entity
from chatbot_graph import ChatBotGraph

app = Flask(__name__)
CORS(app, resources=r'/*')
graph = Graph("bolt://localhost:7687", auth=("neo4j", "bigben123"))
# graph = Graph("http://localhost:7474", password="bigben123")
handler = ChatBotGraph(graph)


@app.route('/search', methods=['get'])
def cal_heart_rate():
    query = request.args.get("entity")
    limit = request.args.get("limit")
    # return jsonify({"q": query})
    if len(query) < 6:
        result = search_entity(query, limit, graph)
        return jsonify({'data': result})
    else:
        answer = handler.chat_main(query)
        return jsonify(answer)


@app.route('/know', methods=['get'])
def know():
    sent = request.args.get("text")
    base_url = 'https://hz-4.matpool.com:27641?token=XoLPRsLpnW'
    url = base_url + '&sent={0}'.format(sent)
    res = requests.get(url).content
    print(type(res))
    print(res)
    return res


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8081, threaded=True)
